import QQMapWX from '../../utils/qqmap-wx-jssdk.min';
const {
  lawyerGetListApi,
  lawyerDetailApi,
  caseInfoGetListApi,
  honorGetListApi,
  fileGetAliOssUrlPathApi,
  commonGetDeptInfoApi
} = require('../../api/index')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoadIntroduct: false,
    lawyerId: '',
    lawyerInfo: {
      lawyerMobile: '',
      lawyerName: '',
      wxCode: '',
      lawyerPhoto: '',
      lawyerId: ""
    },
    deptInfo: {
      telPhone: null,
      deptName: "",
      deptPhoto: "",
      deptAddr: ''
    },
    toolsMenuOptionsData: {
      tel: "",
      addr: ""
    }
  },
  goBack(){
    wx.navigateBack({
      delta:1,
      fail:function(){
        wx.navigateTo({
          url: '/pages/team/team'
        })
      }
    })
  },
  getAllList() {
    const params = { pageNum: 1, pageSize: 4,lawyerId:this.data.lawyerId }
    Promise.all([
      caseInfoGetListApi(params),
      honorGetListApi(params),
      fileGetAliOssUrlPathApi(),
      commonGetDeptInfoApi()
    ]).then(res => {
      res[1].data.list.forEach(t => {
        t.content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/, function (match, capture) {
          t.img = capture.replace('{path}',res[2].data.aliOssUrlPath)
        });
      });
      this.setData({
        caseList: res[0].data.list,
        honorList: res[1].data.list,
        aliOssUrlPath: res[2].data.aliOssUrlPath,
        deptInfo: res[3].data,
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(optios) {
    // 设置分享朋友圈
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    this.setData({
      systemInfo: wx.getMenuButtonBoundingClientRect(),
      lawyerId: optios.id
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (!this.data.lawyerId) {
      lawyerGetListApi({
        deptId: true,
        pageSize: 1,
        pageNum: 1
      }).then((res: { data: { list: { lawyerId: any; }[]; }; }) => {
        if (res.data.list[0]) {
          this.setData({
            lawyerId: res.data.list[0].lawyerId
          })
          this.getLawyerInfo(res.data.list[0].lawyerId)

        } else {
          wx.showToast({
            icon: 'none',
            title: '还没有律师，请前往后台添加律师~'
          })
        }
      })
    } else {
      this.getLawyerInfo()
      this.getAllList()
    }



  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  },
  onShareTimeline: function () {
    return {
      title: this.data.lawyerInfo.lawyerName + "律师-电子名片",
      query: `id=` + this.data.lawyerInfo.lawyerId,
      imageUrl: this.data.lawyerInfo.wxCode || this.data.lawyerInfo.lawyerPhoto,  //用户分享出去的自定义图片大小为5:4,
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: this.data.lawyerInfo.lawyerName + "律师-电子名片",
      imageUrl: this.data.lawyerInfo.wxCode || this.data.lawyerInfo.lawyerPhoto,  //用户分享出去的自定义图片大小为5:4,
      success: function (res: any) {

      },
      fail: function (res: any) {
        // 分享失败
      },
    }
  },

  getLawyerInfo() {
    lawyerDetailApi({
      lawyerId: this.data.lawyerId
    }).then((res: { data: any; }) => {
      res.data.titlePosition=res.data.titlePosition ? res.data.titlePosition.split('|') : []
      res.data.industryBackground=res.data.industryBackground.split('|')
      this.setData({
        lawyerInfo: res.data
      })
    })
  },

  // 拨打律所电话
  toCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.lawyerInfo.lawyerMobile || '',
      success: function () {
      }
    })
    // wx.addPhoneContact({
    //     firstName: this.data.lawyerInfo.lawyerName,
    //     mobilePhoneNumber: this.data.lawyerInfo.lawyerMobile,
    //     success(res) {
    //     }
    //   })
  },
  // 展开更多简介
  loadIntroduction() {
    this.setData({
      isLoadIntroduct: !this.data.isLoadIntroduct
    })
  },
  previewImage(e: any) {
    wx.previewImage({
      urls: e.currentTarget.dataset.list.map((t: { addr: any; }) => t.addr), // 需要预览的图片http链接列表
      current: e.currentTarget.dataset.url, // 当前显示图片的http链接
    })
  },
  toApp() {
    let that = this
    let qqMap = new QQMapWX({
      key: 'PRDBZ-2BXK3-XOD32-YEYA7-SXDD5-RRBOW' // 必填
    });
    qqMap.geocoder({
      address: that.data.deptInfo.deptAddress,   //用户输入的地址（注：地址中请包含城市名称，否则会影响解析效果），如：'北京市海淀区彩和坊路海淀西大街74号',
      complete: (res: { result: { location: { lat: any; lng: any; }; }; }) => {
        if (!res.result) {
          console.log('获取经纬度失败');
        } else {
          wx.openLocation({
            latitude: res.result.location.lat,
            longitude: res.result.location.lng,
            scale: 18,
            name: that.data.deptInfo.deptName,
            address: that.data.deptInfo.deptAddress
          })
        }
      }
    });
  }
})